import { combineReducers } from 'redux';

const initState = [
  {
    id: 0,
    weight: 1233,
    isEaten: false
  },
  {
    id: 1,
    weight: 235,
    isEaten: true
  },
  {
    id: 2,
    weight: 256,
    isEaten: false
  }
];

const apples = (state = initState, action) => {
  switch (action.type) {
    case 'EAT_APPLE':
      const data = state.map(
        m => (m.id === action.appleId ? { ...m, isEaten: !m.isEaten } : m)
      );
      return data
    case 'PICK_APPLE':
      const isPick = true
      setTimeout(() => {
        isPick = false
        let id = state.reduce((sub, item) => {
          return item.id > sub ? item.id : sub;
        }, 0)
        let apple = {
          id: id + 1,
          weight: 1256,
          isEaten: false
        }
        return state.push(apple)
      }, 300);
    default:
      return state;
  }
};

const root = combineReducers({
  apples,
});

export default root;
